package Demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Demo1 {

    private DatagramSocket datagramSocket = null;

    public Demo1(int p) throws SocketException {
        datagramSocket = new DatagramSocket(p);
    }

    public void start() throws IOException {
        System.out.println("服务器上线");
        while (true) {

            DatagramPacket p1 = new DatagramPacket(new byte[4096], 4096);
            datagramSocket.receive(p1);
            String s1 = new String(p1.getData(), 0, p1.getLength());
            String s2 = poccess(s1);
            DatagramPacket p2 = new DatagramPacket(s2.getBytes(), 0, s2.getBytes().length, p1.getSocketAddress());
            datagramSocket.send(p2);
            System.out.printf("[%s:%d] req:%s resp:%s\n", p1.getAddress(), p1.getPort(), s1, s2);


        }
    }

    private String poccess(String s1) {

        return s1;
    }

    public static void main(String[] args) throws IOException {
        Demo1 d = new Demo1(9099);
        d.start();
    }
}
